package com.lh.controller.center;

import com.lh.controller.BaseController;
import com.lh.pojo.vo.OrderStatusCountsVO;
import com.lh.service.center.MyOrderService;
import com.lh.utils.IMOOCJSONResult;
import com.lh.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-26 09:39
 **/
@Api(value = "订单管理相关接口", tags = {"订单管理相关api接口"})
@RestController
@RequestMapping("/myorders")
public class MyOrderController extends BaseController {

    @Autowired
    private MyOrderService myOrderService;

    @ApiOperation(value = "订单查询", notes = "订单查询", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/query")
    public IMOOCJSONResult query(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "订单状态", value = "订单状态", required = false)
            @RequestParam Integer orderStatus,
            @ApiParam(name = "页数", value = "页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页条数", value = "每页条数", required = false)
            @RequestParam Integer pageSize
    ){
        if (StringUtils.isBlank(userId)){
            return IMOOCJSONResult.errorMsg("用户Id不能为空!");
        }

        if (page == null){
            page = 1;
        }

        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }


        PagedGridResult pagedGridResult = myOrderService.queryMyOrdersVO(userId, orderStatus, page, pageSize);

        return IMOOCJSONResult.ok(pagedGridResult);

    }

    // 商家发货没有后端，所以这个接口仅仅只是用于模拟
    @ApiOperation(value="商家发货", notes="商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public IMOOCJSONResult deliver(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId){

        if (StringUtils.isBlank(orderId)) {
            return IMOOCJSONResult.errorMsg("订单ID不能为空");
        }
        myOrderService.updateDeliverOrderStatus(orderId);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value="买家确认收货", notes="买家确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public IMOOCJSONResult confirmReceive(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId){
        if (StringUtils.isBlank(userId)){
            return IMOOCJSONResult.errorMsg("用户Id不能为空!");
        }
        if (StringUtils.isBlank(orderId)){
            return IMOOCJSONResult.errorMsg("订单Id不能为空!");
        }

        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean b = myOrderService.confirmReceive(orderId);

        if (!b){
            return IMOOCJSONResult.errorMsg("收货失败！");
        }

        return IMOOCJSONResult.ok();

    }
    @ApiOperation(value="买家删除订单", notes="买家删除订单", httpMethod = "POST")
    @PostMapping("/delete")
    public IMOOCJSONResult delete(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("用户Id不能为空!");
        }
        if (StringUtils.isBlank(orderId)) {
            return IMOOCJSONResult.errorMsg("订单Id不能为空!");
        }

        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean delete = myOrderService.delete(userId, orderId);

        if (!delete){
            return IMOOCJSONResult.errorMsg("删除失败！");
        }

        return IMOOCJSONResult.ok();

    }

    @ApiOperation(value="查找订单状态", notes="查找订单状态", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public IMOOCJSONResult statusCounts(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {
        if (StringUtils.isBlank(userId)){
            return IMOOCJSONResult.errorMsg("用户id不能为空!");
        }

        OrderStatusCountsVO orderStatusCountsVO = myOrderService.statusCounts(userId);

        return IMOOCJSONResult.ok(orderStatusCountsVO);
    }

    @ApiOperation(value="订单动向", notes="订单动向", httpMethod = "POST")
    @PostMapping("/trend")
    public IMOOCJSONResult trend(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "页数", value = "页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页条数", value = "每页条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)){
            return IMOOCJSONResult.errorMsg("用户Id不能为空!");
        }

        if (page == null){
            page = 1;
        }

        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }

        PagedGridResult trend = myOrderService.trend(userId, page, pageSize);
        return IMOOCJSONResult.ok(trend);
    }
}
